/**
 * @program: LeetCode
 * @description: LeetCode : 剑指 Offer 53 - I. 在排序数组中查找数字 I
 * @author: WXY
 * @create: 2022-12-09 20:15
 * @Version 1.0
 **/


public class Num053_1_search {

    public static int search(int[] nums, int target) {
        int left = 0;
        int right = nums.length - 1;
        int mid = 0;
        while (left < right) {
            mid = left + (right - left) / 2;
            if (nums[mid] < target) {
                left = mid  + 1 ;
            } else {
                right = mid  ;
            }
        }
        int ans = 0;
        while (left < nums.length) {
            if (nums[left++] == target) {
                ans++;
            }
        }
        return ans;
    }
    public static int search1(int[] nums, int target) {
        int left = 0;
        int right = nums.length - 1;
        int mid = 0;
        while (left <= right) {
            mid = left + (right - left) / 2;
            if (nums[mid] < target) {
                left = mid  + 1 ;
            } else {
                right = mid - 1  ;
            }
        }
        int l = 0;
        int r = nums.length - 1;
        int m = 0;
        while (l <= r) {
            m = l + (r - l) / 2;
            if (nums[m] > target) {
                r = m - 1;
            } else {
                l = m + 1 ;
            }
        }
        return l - right - 1;
    }

    public static void main(String[] args) {
        int[] arr = {0, 1, 2, 3, 3, 3, 3,4, 4, 5, 6, 7, 8, 9};
        int search = search1(arr, 3);
        System.out.println(search);

    }
}
